package com.glory.hikvisionmobile.bean.net;

import android.content.Context;
import android.util.Log;

import androidx.fragment.app.FragmentManager;

import com.glory.hikvisionmobile.base.App;
import com.glory.hikvisionmobile.bean.Exception;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;

public class BaseResponseBean<T> implements Serializable {

    private ReturnBean Header;

    private T Body;

    private transient FreeUI_GeneralFragmentDialog tipDialog;
    public T getBody() {
        return Body;
    }

    public void setBody(T body) {
        this.Body = body;
    }

    public ReturnBean getHeader() {
        return Header == null ? new ReturnBean() : Header;
    }

    public void setHeader(ReturnBean Header) {
        this.Header = Header;
    }

     //获取到对象后，必须要判断是否获取成功
     public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager fragmentManager) {
         if (Header.getResult().toUpperCase().equals("SUCCESS")) {
             if (isHaveBody) {
                 if (Body == null || Body.equals("")) {
                     showFailDialog("暂无请求内容",fragmentManager);
                     return false;
                 } else {
                     if (Body instanceof ArrayList) {
                         if ((((ArrayList) Body).size()) > 0) {
                             return true;
                         } else {
                             showFailDialog("暂无请求数据",fragmentManager);
                             return false;
                         }
                     } else {
                         return true;
                     }
                 }
             } else {
                 return true;
             }
         } else {
             showFailDialog(getMessage(Header.getResultCode()),fragmentManager);
             return false;
         }
     }


     private void showFailDialog(String message,FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "确定", message, () -> {

        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

     private String getMessage(String resultCode){
         String message = "";
         Exception e = new Exception();
         String[] result_code_en = e.getResult_code_en();
         String[] result_message = e.getResult_message_ch();
         for (int i = 0; i < result_code_en.length; i++) {
             if (result_code_en[i].equals(resultCode)){
                 message = result_message[i];
                 break;
             }

         }
         if (FreeApi_StringUtils.isEmpty(message)){
             message = resultCode;
         }
         return message;
     }




}
